#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "1.h"


static void __sort(void *arr, int left, int right, int size, cmp_t cmp);
void myqsort(void *arr, int nmemb, int size, cmp_t cmp)
{
	__sort(arr, 0, nmemb - 1, size, cmp);	
}

static void __sort(void *arr, int left, int right, int size, cmp_t cmp){
	if(left < right){
		void *base;
		int i = left;
		int j = right;

		base = malloc(size);
		memcpy(base, (char *)arr + left * size, size);
		for(; i < j;){
			while(i < j && cmp(base, (char *)arr + j *size) <= 0){
				j--;
			}
			memcpy((char *)arr + i * size, (char *)arr + j * size, size);
		
			while(i < j && cmp(base, (char *)arr + i * size) >= 0){
				i++;
			}
			memcpy((char *)arr + j * size, (char *)arr + i * size, size);
	
		}
		memcpy((char *)arr + i * size, base, size);
		free(base);
		base = NULL;	

		__sort(arr, left, i - 1, size, cmp);
		__sort(arr, j + 1, right, size, cmp);

	}
	return;
}

void show_arr(void *arr, int nmemb, int size, pri_t pri)
{
	int i;

	for (i = 0; i < nmemb; i++) {
		pri((char *)arr + i * size);
	}
}


